﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.IO.IsolatedStorage;
using System.IO;
using System.Text;

namespace Samples.SimpleStreaming
{
    public partial class TraceLogPage
    {

        public static readonly Mutex TracerMutex = new Mutex(false, "TraceLogger");
        public const string LogFileName = "Streaming.Tracer.txt";
        private readonly List<string> log = new List<string>();
        private int currentPage;
        private const int PageLen = 24;

        public TraceLogPage()
        {
            InitializeComponent();
            LoadTraceLog();
            ShowPage();
        }

        private void ShowPage()
        {
            int lineCnt = 0;
            var sb = new StringBuilder();
            while (lineCnt < PageLen && (currentPage*PageLen) + lineCnt < log.Count)
            {
                sb.Append(log[(currentPage*PageLen) + lineCnt] + Environment.NewLine);
                lineCnt++;
            }
            txtTrace.Text = sb.ToString();
        }

        private void LoadTraceLog()
        {
            log.Clear();
            try
            {
                TracerMutex.WaitOne();

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    try
                    {
                        if (store.FileExists(LogFileName))
                        {
                            using (
                                var istream = new IsolatedStorageFileStream(LogFileName, FileMode.Open,
                                                                            FileAccess.Read, FileShare.Read, store))
                            using (var sr = new StreamReader(istream))
                            {
                                while (sr.Peek() >= 0)
                                {
                                    log.Add(sr.ReadLine());
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                TracerMutex.ReleaseMutex();
            }
            txtStatus.Text = log.Count.ToString(CultureInfo.InvariantCulture) + " Trace Records";
        }

        private void BackPageClick(object sender, EventArgs e)
        {
            if (currentPage == 0)
            {
                currentPage = (log.Count/PageLen) + 1;
            }
            if (currentPage > 0)
            {
                currentPage--;
            }
            ShowPage();
        }

        private void ForwardPageClick(object sender, EventArgs e)
        {
            if (currentPage*PageLen > log.Count)
            {
                currentPage = 0;
            }
            else
            {
                currentPage++;
            }
            ShowPage();
        }

        private void RefreshClick(object sender, EventArgs e)
        {
            LoadTraceLog();
        }

        private void BeginningClick(object sender, EventArgs e)
        {
            currentPage = 0;
            ShowPage();
        }

        private void EndClick(object sender, EventArgs e)
        {
            LoadTraceLog();
            currentPage = (log.Count/PageLen);
            ShowPage();
        }
    }
}